
import UIKit
import Alamofire
import HandyJSON

//MARK: Global Define

public class JXExt {
    
    //MARK: - RANDOM NUMBER
    public static func randomIntNumber(lower: Int = 0,upper: Int = Int(UInt32.max)) -> Int {
        return lower + Int(arc4random_uniform(UInt32(upper - lower)))
    }

    public static func randomIntNumber(range: Range<Int>) -> Int {
        return randomIntNumber(lower: range.lowerBound, upper: range.upperBound)
    }
    
    public static func randomFloatNumber(lower: Float = 0,upper: Float = 100) -> Float {
        return (Float(arc4random()) / Float(UInt32.max)) * (upper - lower) + lower
    }
    
    public static func randomCGFloatNumber(lower: CGFloat = 0,upper: CGFloat = 1) -> CGFloat {
        return CGFloat(Float(arc4random()) / Float(UInt32.max)) * (upper - lower) + lower
    }
    
    public static func randomDoubleNumber(lower: Double = 0,upper: Double = 100) -> Double {
        return (Double(arc4random())/Double(UInt32.max))*(upper - lower) + lower
    }
    
    
    static func adjustFont(fontSize:CGFloat) -> (UIFont){
        
        let font =  UIFont.systemFont(ofSize: fontSize * scaleCoefficient4_7_INCH)
        
        return font
    }
    
    
    static func getBundle(bundleName:String) -> (Bundle?) {
        
        let BundlePath = Bundle.main.path(forResource: bundleName , ofType: "bundle")
        
        let bundle = Bundle(path: BundlePath ?? "")
        
        return bundle ?? nil
    }
    
    
    func loadVcFromeStoryBoard(BundleName:String?,SbName:String,nibName:String) -> UIViewController? {
        
        let bundle = BundleName == nil ? Bundle.main : JXExt.getBundle(bundleName: BundleName!)
        
        let sb  = UIStoryboard.init(name: SbName , bundle: bundle)
        let vc = sb.instantiateViewController(withIdentifier: nibName)
        
        return vc
        
    }
    
    func loadViewFromNib(BundleName:String?,nibName:String,isVC:Bool) -> Any? {
        
        let bundle = BundleName == nil ? Bundle.main : JXExt.getBundle(bundleName: BundleName!)
        
        if isVC == true{
            return bundle?.loadNibNamed(nibName, owner: nil , options: [:])?.last as? UIViewController
        }
        
        return bundle?.loadNibNamed(nibName, owner: nil , options: [:])?.last as? UIView
    }
    
    
    func createQR_Code(dataString:String,size:CGSize) -> UIImage?{
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        // 2.恢复默认设置
        filter?.setDefaults()
        // 3.设置数据
        let info = dataString
        let infoData: Data? = info.data(using: .utf8)
        filter?.setValue(infoData, forKeyPath: "inputMessage")
        // 4.生成二维码
        let outputImage: CIImage? = filter?.outputImage
        
        if let ciImage  = outputImage {
            
            return ciImage.ext.createClearImage(ciImage , width: size.width)
        }
        
        return nil
    }
    
}






